<template>
  <div class="application-record-list">
    <div class="breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item to="Home" :replace="true">首页</el-breadcrumb-item>
        <el-breadcrumb-item>应聘申请列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :inline="true" :model="searchParams">
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input v-model="searchParams.enterpriseName" placeholder="请选择企业名称" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findEnterprise"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="招聘岗位名称" prop="jobDescriptionName">
        <el-input v-model="searchParams.jobDescriptionName" placeholder="请选择招聘岗位名称" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findJobDescription"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="应聘者姓名" prop="candidateName" v-if="findCandidateFormItem">
        <el-input v-model="searchParams.candidateName" placeholder="请选择应聘者姓名" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findCandidate"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="searchParams.status" placeholder="请选择状态">
          <el-option label="申请" :value="10"></el-option>
          <el-option label="面试邀约" :value="20"></el-option>
          <el-option label="录用" :value="30"></el-option>
          <el-option label="拒绝" :value="90"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button type="primary" @click="onAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="pageData.list"
      border
      v-loading="pageData.loading"
      size="mini"
      header-cell-class-name="table-header-cell-font"
      cell-class-name="table-cell-font">
      <el-table-column prop="enterpriseName" label="企业名称"></el-table-column>
      <el-table-column prop="jobDescriptionName" label="招聘岗位名称"></el-table-column>
      <el-table-column prop="candidateName" label="应聘者姓名"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="{ row }">
          <div v-if="row.status === 10">申请</div>
          <div v-if="row.status === 20">面试邀约</div>
          <div v-if="row.status === 30">录用</div>
          <div v-if="row.status === 90">拒绝</div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="350">
        <template v-slot="{ row }">
          <el-button @click.native.prevent="editRow(row.id)" type="primary">编辑</el-button>
          <el-button @click.native.prevent="delRow(row.id)" type="danger" plain>删除</el-button>
          <el-button @click.native.prevent="detailRow(row.id)" type="primary" plain>详情</el-button>
          <el-button @click.native.prevent="candidateRow(row.candidateId)" type="primary" plain>应聘者详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination">
      <el-pagination
        @current-change="paginationChange"
        :current-page="pageData.current"
        :page-sizes="pageData.pageSizes"
        :page-size="pageData.size"
        background
        layout="total, prev, pager, next, jumper"
        :total="pageData.count">
      </el-pagination>
    </div>
    <el-dialog :visible.sync="enterpriseSelectorVisible" v-if="enterpriseSelectorVisible" title="企业信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <enterprise-selector @enterprise-selected-event="handleEnterpriseSelectedEvent">
      </enterprise-selector>
    </el-dialog>
    <el-dialog :visible.sync="jobDescriptionSelectorVisible" v-if="jobDescriptionSelectorVisible" title="招聘岗位选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <job-description-selector @job-description-selected-event="handleJobDescriptionSelectedEvent">
      </job-description-selector>
    </el-dialog>
    <el-dialog :visible.sync="candidateSelectorVisible" v-if="candidateSelectorVisible" title="应聘者基本信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <candidate-selector @candidate-selected-event="handleCandidateSelectedEvent">
      </candidate-selector>
    </el-dialog>
    <el-dialog :visible.sync="applicationRecordAddVisible" v-if="applicationRecordAddVisible" :title="applicationRecordAddTitle" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <application-record-add :id="selectedApplicationRecordId" @close-application-record-add-event="handleCloseApplicationRecordAddEvent" @reset-application-record-add-event="handleResetApplicationRecordAddEvent">
      </application-record-add>
    </el-dialog>
    <el-dialog :visible.sync="applicationRecordViewVisible" v-if="applicationRecordViewVisible" title="应聘申请详情" :top="dialogTop" :width="dialogWidth">
      <application-record-view :id="selectedApplicationRecordId" @close-application-record-view-event="handleCloseApplicationRecordViewEvent">
      </application-record-view>
    </el-dialog>
    <el-dialog :visible.sync="candidateViewAllVisible" v-if="candidateViewAllVisible" title="应聘者详情" :top="dialogTop" :width="dialogWidth">
      <candidate-view-all :id="selectedCandidateId" @close-candidate-view-all-event="handleCandidateViewAllEvent">
      </candidate-view-all>
    </el-dialog>
  </div>
</template>

<script>
import applicationRecordApi from '@/api/applicationRecordApi'
import EnterpriseSelector from "@/views/enterprise/EnterpriseSelector.vue";
import JobDescriptionSelector from "@/views/jobDescription/JobDescriptionSelector.vue";
import CandidateSelector from "@/views/candidate/CandidateSelector.vue";
import ApplicationRecordAdd from "@/views/applicationRecord/ApplicationRecordAdd.vue"
import ApplicationRecordView from "@/views/applicationRecord/ApplicationRecordView.vue"
import listQueryMixin from '@/mixins/listQueryMixin'
import CandidateViewAll from "@/views/candidate/CandidateViewAll.vue";
export default {
  name: 'ApplicationRecordList',
  components: {
    CandidateViewAll,
    EnterpriseSelector,JobDescriptionSelector,CandidateSelector,ApplicationRecordAdd, ApplicationRecordView},
  mixins: [
    listQueryMixin
  ],
  data () {
    return {
      searchParams: {
        enterpriseId: '',
        enterpriseName: '',
        jobDescriptionId: '',
        jobDescriptionName: '',
        candidateId: '',
        candidateName: '',
        status: ''
      },
      enterpriseSelectorVisible: false,
      jobDescriptionSelectorVisible: false,
      candidateSelectorVisible: false,
      applicationRecordAddVisible: false,
      applicationRecordAddTitle: '',
      applicationRecordViewVisible: false,
      selectedApplicationRecordId: 0,
      candidateViewAllVisible: false,
      selectedCandidateId: 0,
      selectedEducationId: 0,
      findCandidateFormItem: true
    }
  },
  created () {
    this.initByType()
  },
  methods: {
    getPageData (p) {
      return applicationRecordApi.find(p)
    },
    onSearch () {
      this._getPageData()
    },
    onReset () {
      this.searchParams = {
        enterpriseId: '',
        enterpriseName: '',
        jobDescriptionId: '',
        jobDescriptionName: '',
        candidateId: '',
        candidateName: '',
        status: ''
      }
      this.initByType()
      this.pageData.pageIndex = 1
      this._getPageData()
    },
    onAdd () {
      this.selectedApplicationRecordId = ''
      this.applicationRecordAddVisible = true
      this.applicationRecordAddTitle = '应聘申请新增'
    },
    editRow (id) {
      this.selectedApplicationRecordId = id
      this.applicationRecordAddVisible = true
      this.applicationRecordAddTitle = '应聘申请编辑'
    },
    delRow (id) {
      if (!id) {
        return
      }
      this.$confirm('确定要删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        applicationRecordApi.del(id)
          .then((resp) => {
            if (resp && resp.code === 1) {
              this.$message.success('删除成功!')
              this.onSearch()
            } else {
              this.$message.error('删除失败:' + (resp.msg ? resp.msg : '原因未知'))
            }
          })
          .catch(error => {
            console.error('操作失败:', error)
          })
      }).catch(() => {})
    },
    detailRow (id) {
      this.selectedApplicationRecordId = id
      this.applicationRecordViewVisible = true
    },
    findEnterprise () {
      this.enterpriseSelectorVisible = true
    },
    handleEnterpriseSelectedEvent (selectedEnterprise) {
      this.enterpriseSelectorVisible = false
      if (selectedEnterprise && 'enterpriseId' in selectedEnterprise && this.searchParams.enterpriseId !== selectedEnterprise['enterpriseId']) {
        this.searchParams.enterpriseId = selectedEnterprise['enterpriseId']
        this.searchParams.enterpriseName = selectedEnterprise['enterpriseName']
      }
    },
    findJobDescription () {
      this.jobDescriptionSelectorVisible = true
    },
    handleJobDescriptionSelectedEvent (selectedJobDescription) {
      this.jobDescriptionSelectorVisible = false
      if (selectedJobDescription && 'jobDescriptionId' in selectedJobDescription && this.searchParams.jobDescriptionId !== selectedJobDescription['jobDescriptionId']) {
        this.searchParams.jobDescriptionId = selectedJobDescription['jobDescriptionId']
        this.searchParams.jobDescriptionName = selectedJobDescription['jobDescriptionName']
      }
    },
    findCandidate () {
      this.candidateSelectorVisible = true
    },
    handleCandidateSelectedEvent (selectedCandidate) {
      this.candidateSelectorVisible = false
      if (selectedCandidate && 'candidateId' in selectedCandidate && this.searchParams.candidateId !== selectedCandidate['candidateId']) {
        this.searchParams.candidateId = selectedCandidate['candidateId']
        this.searchParams.candidateName = selectedCandidate['candidateName']
      }
    },
    handleCloseApplicationRecordViewEvent () {
      this.applicationRecordViewVisible = false
    },
    handleResetApplicationRecordAddEvent () {
      this.applicationRecordAddTitle = '应聘申请新增'
    },
    handleCloseApplicationRecordAddEvent (params) {
      if (params && params.search) {
        this.onSearch()
      }
      this.applicationRecordAddVisible = false
    },
    candidateRow (candidateId) {
      this.selectedCandidateId = candidateId
      this.candidateViewAllVisible = true
    },
    handleCandidateViewAllEvent () {
      this.candidateViewAllVisible = false
    },
    initByType() {
      const type = sessionStorage.getItem('type')
      if (type && Number(type) === 3) {
        this.searchParams.candidateId = 2
        this.findCandidateFormItem = false
      }
    }
  },
}
</script>

<style lang="scss">
</style>